package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.SanphamModel;
import dao.GetCnn;

/**
 * Servlet implementation class quanlyncc
 */
@WebServlet("/quanlyncc")
public class quanlyncc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public quanlyncc() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
    	String ten = request.getParameter("ten");
    	ten = new String(ten.getBytes("8859_1"),"UTF8");
    	String diachi = request.getParameter("diachi");
    	diachi = new String(diachi.getBytes("8859_1"),"UTF8");
    	String dienthoai = request.getParameter("dienthoai");
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
    	String option = request.getParameter("option");
    	int mancc = 0;
    	String sSQL = "";
    	if(option.equals("addnew")){
    		SanphamModel model = new SanphamModel();
    		mancc = model.getTotalRows("select * from nhacungcap")+1;
    		sSQL ="insert into nhacungcap(MANCC,TEN,DIACHI,DIENTHOAI) values('"+mancc+"',N'"+ten+"',N'"+diachi+"','"+dienthoai+"')";
    		stat.executeUpdate(sSQL);
    	}
    	response.sendRedirect("adminncc");
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
